﻿using System;
using System.Collections.Generic;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.processor.type.comm;
using SharpSqlAnalyzer.pp.utils;
using SharpSqlAnalyzer.stmt;

namespace SharpSqlAnalyzer.pp.processor.type.merge
{
	// Token: 0x0200023C RID: 572
	public class MergeWhenClauseProcessor : AbstractProcessor
	{
		// Token: 0x060012EB RID: 4843 RVA: 0x0031B890 File Offset: 0x00319A90
		public override void process(TParseTreeNode nodeArg)
		{
			TMergeSqlStatement tmergeSqlStatement = nodeArg as TMergeSqlStatement;
			if (tmergeSqlStatement.WhenClauses != null)
			{
				List<TMergeWhenClause> whenClauses = tmergeSqlStatement.WhenClauses;
				for (int i = 0; i < whenClauses.Count; i++)
				{
					TMergeWhenClause clause = whenClauses[i];
					this.processMergeWhenClause(tmergeSqlStatement, clause);
				}
			}
		}

		// Token: 0x060012EC RID: 4844 RVA: 0x0031B8D4 File Offset: 0x00319AD4
		private void processMergeWhenClause(TMergeSqlStatement sql, TMergeWhenClause clause)
		{
			int length = SourceTokenOperator.curColumnNumberVT(this.getMergeToken(sql));
			TSourceToken tsourceToken = SourceTokenSearcher.forwardSearch(clause.startToken, 5, "when");
			SourceTokenOperator.removeWhitespaceAndReturnFromEnd(this.Option, tsourceToken);
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createReturnSourceToken());
			SourceTokenOperator.addBefore(this.Option, tsourceToken, SourceTokenOperator.createWhitespaceSourceToken(length));
		}

		// Token: 0x060012ED RID: 4845 RVA: 0x0031B930 File Offset: 0x00319B30
		private TSourceToken getMergeToken(TMergeSqlStatement node)
		{
			TSourceTokenList container = node.startToken.container;
			for (int i = 0; i < container.size(); i++)
			{
				TSourceToken tsourceToken = container.get(i);
				if (tsourceToken.ToString().Equals("merge", StringComparison.CurrentCultureIgnoreCase))
				{
					return tsourceToken;
				}
			}
			return null;
		}
	}
}
